﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 数据接口实现。
    /// </summary>
    /// 创建时间：2022-12-14 10:25:15。作者：王浩力
    public class Clean_gas_check_historyDAL_impl : BaseDAL, IClean_gas_check_historyDAL
    {
        /// <summary>
        /// 审核处理
        /// </summary>
        /// <param name="history"></param>
        /// <returns></returns>
        public IResult Check(Clean_gas_check_history history)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            string sql = $@"INSERT INTO clean_gas_check_history (
                                        id,
                                        check_user_name,
                                        check_user_id,
                                        check_time,
                                        check_stat,
                                        reject_reason,
                                        jing_hua_qi_key
                                    )
                                    VALUES(
                                        {snowflake.NextId()},
                                        $check_user_name,
                                        $check_user_id,
                                        $check_time,
                                        $check_stat,
                                        $reject_reason,
                                       {history.Jing_hua_qi_key}
                                    );
            ";
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$check_user_name", history.Check_user_name);
            keyValuePairs.Add("$check_user_id", history.Check_user_id);
            keyValuePairs.Add("$check_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            keyValuePairs.Add("$check_stat", history.Check_stat);
            keyValuePairs.Add("$reject_reason", history.Reject_reason);

            //主表修改步骤值。同一个sql，执行效果为事务
            byte step = 3; //驳回
            if (history.Check_stat == 1)
            {
                step = 4; //通过
            }
            sql += $"update   clean_gas  set  o_step={step}  where  id={history.Jing_hua_qi_key}; ";
            string error = null;
            int count = ExecSqlCmd(sql, keyValuePairs, c =>
             {
                 return c.ExecuteNonQuery();
             }, exp => { error = exp.Message; });
            if (count > 0)
            {
                return new ResultSuccess("审核成功");
            }
            return new Result("审核异常，" + error);
        }

        public List<Clean_gas_check_history> GetList(long cleanGasKEY)
        {
            string sql = "select   *    from  Clean_gas_check_history  where  jing_hua_qi_key=" + cleanGasKEY;
            return ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Clean_gas_check_history> list = new List<Clean_gas_check_history>();
                while (reader.Read())
                {
                    Clean_gas_check_history info = new Clean_gas_check_history();
                    info.Id = (long)reader["Id"].ToString().ToMyDecimal();
                    info.Check_user_name = reader["Check_user_name"].ToString();
                    info.Check_user_id = (long)reader["Check_user_id"].ToString().ToMyDecimal();
                    info.Check_time = reader["Check_time"].ToString();
                    info.Check_stat = (byte)reader["Check_stat"].ToString().ToMyDecimal();
                    info.Reject_reason = reader["Reject_reason"].ToString();
                    info.Jing_hua_qi_key = (long)reader["Jing_hua_qi_key"].ToString().ToMyDecimal();
                    list.Add(info);
                }
                return list;
            });

        }

    }
}
